package com.yinhe.module.ls.controller;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhBaseinfo;
import com.yinhe.system.domain.YhBaseinfoFamily;
import com.yinhe.system.service.ISysPostService;
import com.yinhe.system.service.ISysUserService;
import com.yinhe.system.service.IYhBaseinfoFamilyService;
import com.yinhe.system.service.IYhBaseinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 基本信息
 *
 */
@RestController
@RequestMapping("/ls/baseinfo")
public class LsBaseinfoController extends BaseController {
    @Autowired
    private IYhBaseinfoService yhBaseinfoService;
    @Autowired
    private ISysPostService postService;
    @Autowired
    private IYhBaseinfoFamilyService yhBaseinfoFamilyService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('module:ls:baseinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhBaseinfo yhBaseinfo)
    {
        startPage();
        List<YhBaseinfo> list = yhBaseinfoService.selectYhBaseinfoList(yhBaseinfo);
        return getDataTable(list);
    }

    /**
     * 导出基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:baseinfo:export')")
    @Log(title = "基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhBaseinfo yhBaseinfo)
    {
        List<YhBaseinfo> list = yhBaseinfoService.selectYhBaseinfoList(yhBaseinfo);
        ExcelUtil<YhBaseinfo> util = new ExcelUtil<YhBaseinfo>(YhBaseinfo.class);
        util.exportExcel(response, list, "基本信息数据");
    }

    /**
     * 获取基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:baseinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhBaseinfoService.selectYhBaseinfoById(id));
    }



    /**
     * 新增基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:baseinfo:add')")
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhBaseinfo yhBaseinfo)
    {
        // 存入userId和deptId，做数据权限
        yhBaseinfo.setUserId(Long.parseLong(yhBaseinfo.getIdNumber()));

//        List<YhBaseinfo> yhBaseinfo1 =  yhBaseinfoService.selectUserById();

        SysUser sysUser =  userService.selectByIdNumber(yhBaseinfo.getIdNumber());
        yhBaseinfo.setUserId(sysUser.getUserId());
        yhBaseinfo.setDeptId(sysUser.getDeptId());

        System.out.println(yhBaseinfo+"666");
        return toAjax(yhBaseinfoService.insertYhBaseinfo(yhBaseinfo));
    }
    /**
     * 新增亲属基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:baseinfo:add')")
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/familyAdd")
    public AjaxResult familyadd(@RequestBody YhBaseinfo yhBaseinfo)
    {
        YhBaseinfoFamily family = new YhBaseinfoFamily();
        family.setUsername(yhBaseinfo.getUsername());
       // family.setCertificateId(yhBaseinfo.getCertificateId());
        family.setIdNumber(yhBaseinfo.getIdNumber());
        //family.setDeptid(yhBaseinfo.getDeptid());
        return toAjax(yhBaseinfoService.insertYhBaseinfoFamily(family));
    }

    /**
     * 修改基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:baseinfo:edit')")
    @Log(title = "基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhBaseinfo yhBaseinfo)
    {
        return toAjax(yhBaseinfoService.updateYhBaseinfo(yhBaseinfo));
    }

    /**
     * 删除基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:baseinfo:remove')")
    @Log(title = "基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhBaseinfoService.deleteYhBaseinfoByIds(ids));
    }



    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        System.out.println("=============come==============");
        ExcelUtil<YhBaseinfo> util = new ExcelUtil<YhBaseinfo>(YhBaseinfo.class);
        util.importTemplateExcel(response, "用户数据");
    }

   @Log(title = "信息导入", businessType = BusinessType.IMPORT)
   @PreAuthorize("@ss.hasPermi('system:baseinfo:import')")
   @PostMapping("/importData")
   public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        //System.out.println("===============double=================");
        ArrayList<YhBaseinfo> result = new ArrayList<>();
        ExcelUtil<YhBaseinfo> util = new ExcelUtil<YhBaseinfo>(YhBaseinfo.class);
        List<YhBaseinfo> userList = util.importExcel(file.getInputStream());
        for (YhBaseinfo a:userList){
          SysUser user =    userService.selectByIdNumber(a.getIdNumber());
          a.setUserId(user.getUserId());
          a.setDeptId(user.getDeptId());
          result.add(a);
        }
        String operName = getUsername();
        String message = yhBaseinfoService.importBaseinfo(result, updateSupport,operName);
        return AjaxResult.success(message);
    }

}



